import request from './request';
import qs from 'qs';
const baseUrl = 'https://open.ys7.com';

import moment from 'moment';

// 设置基本的opthions
function setOptions(method = 'GET', param = {}) {
  let query = '';
  // const token = localStorage.getItem('ys7')
  // const token1 = 'eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJsZW9wYXJkIiwianRpIjoiMzAiLCJzdWIiOiJzdXBlckFkbWluIiwiaWF0IjoxNTk2NTA0Nzk1LCJleHAiOjE1OTY1NDc5OTV9.vNqHMREAtt9xLHYNM3HozMWvvSf0L4VtEbPvhV6IrnMFenjeaXu-IOHLojGNtNHfxFcSzINzQfnzbWqpMpRaOg'
  const options = {
    method: method,
    headers: {
      // 'Content-Type': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      // Authorization: token ? token : token1,
    },
    // handleAs: 'json',
  };
  if (method.toLowerCase() === 'post') {
    options.body = qs.stringify(param);
  }
  // else if (method.toLowerCase() === 'get') {
  //   query = qs.stringify(param);
  // }
  return options;
}

export function getYS7Token() {
  try {
    let data = JSON.parse(localStorage.getItem('YS7'));

    if (data.expireTime > Date.now()) {
      return Promise.resolve();
    }
    throw new Error();
  } catch (err) {
    const options = setOptions('post', {
      appKey: '5d62e55550a74ed09feb318c65a8400a',
      appSecret: 'd8d543ef513e57a5992f7c11b8de3011',
    });

    return request(`${baseUrl}/api/lapp/token/get`, options).then(({ data }) => {
      console.log(data.data);
      localStorage.setItem('YS7', JSON.stringify(data.data));
      return data.data;
    });
  }
}

export function getLiveList(pageSize = 50, pageStart = 0) {
  try {
    let { accessToken } = JSON.parse(localStorage.getItem('YS7'));
    const options = setOptions('post', {
      accessToken,
      pageSize,
      pageStart,
    });
    return request(`${baseUrl}/api/lapp/live/video/list`, options).then(({ data }) => {
      console.log(data.data);
      return data.data;
    });
  } catch (err) {}
}
